<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box1 {
        width: 100px;
        height: 100px;
        background: red;
        display: block;
      }

      .box2 {
        width: 100px;
        height: 100px;
        background: blue;
        display: none;
      }

      .box3 {
        width: 100px;
        height: 100px;
        background: yellow;
        display: none;
      }
    </style>
  </head>

  <body>
    <div class="contaner1">
      <button>按钮一</button>
      <button>按钮二</button>
      <button>按钮三</button>
      <div class="box1">1</div>
      <div class="box2">2</div>
      <div class="box3">3</div>
    </div>
    <button class="next">下一页</button>

    <div class="contaner2">
      <button>按钮一</button>
      <button>按钮二</button>
      <button>按钮三</button>
      <div class="box1">1</div>
      <div class="box2">2</div>
      <div class="box3">3</div>
    </div>
    <script>
      // 面向对象：简单的程序 面向过程；
      // 面向对象：需求复杂(属性方法比较多) 涉及对象较多 。
      // 需求一：写一个选项卡；

      function tab(btns, divs, isone) {
        btns.forEach((btn, key) => {
          btn.onclick = function () {
            switchTab(key);
          };
        });

        // 可以切换到指定的key的div上
        function switchTab(key) {
          divs.forEach((div, index) => {
            if (key == index) {
              div.style.display = "block";
            } else {
              div.style.display = "none";
            }
          });
        }

        // 不好
        if (isone) {
          // 第一个选项卡
          var nextBtn = document.querySelector(".next");
          var num = 0;
          nextBtn.onclick = function () {
            switchTab(num);
            num++;
            if (num > 2) {
              num = 0;
            }
          };
        }
      }

      var btns = document.querySelectorAll(".contaner1 button");
      var divs = document.querySelectorAll(".contaner1 div");
      tab(btns, divs, true);

      var btns2 = document.querySelectorAll(".contaner2 button");
      var divs2 = document.querySelectorAll(".contaner2 div");

      tab(btns2, divs2);
    </script>
  </body>
</html>
